#ifndef UANALYSERCHANNEL_H
#define UANALYSERCHANNEL_H

#include <QtGui>

class uAnalyserWidget;
class uAnalyserRenderer;
class uAnalyserControls;

//class responsible for drawing input signal on screen
class uAnalyserChannelTrace : public QGraphicsItem
{
public:
    uAnalyserChannelTrace(QVector<double> *time_sweep, QVector<double> *voltage, double gain, double t_base, QColor col);
    QRectF boundingRect() const {  return boundRect;  }
    QPainterPath shape() const {  return shapePath;  }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    //calculate from contents of "voltage" geometric shapes used later by boundingRect(), shape() and paint(...)
    void calculatePainterPath();

    void setColor(QColor col) { color = col; }
    void setYGain(double y_gain) { yGain = y_gain; }
    void setTimeBase(double t_base) { timeBase = t_base; }

    //mouse events are used here for dragging traces along vertical axis. Modify double yOffset;
    void mousePressEvent( QGraphicsSceneMouseEvent * event ) {}
    void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
    void mouseReleaseEvent( QGraphicsSceneMouseEvent * event ) {}

private:
    QVector<double> *timeSweep; //owned by uAnalyserWidget. Timestamps of particular samples
    QVector<double> *voltage; //owned by uAnalyserChannel. Input signal samples
    double yGain; //Y Gain (volts/div).
    double timeBase; //time base (seconds/div).
    double yOffset; //offset of trace (div)
    QColor color; //color of trace.
    QPainterPath painterPath; //shape painted on the screen
    QPainterPath shapePath; //shape used to determine if trace was clicked with mouse
    QRectF boundRect; //rectangle bounding whole trace - necessary for correct screen updating
};

//class responsible for:
// - handling channel signal
// - supplying necessary user interface
// - interfacing with other classes inside application (uAnalyserWidget, uAnalyserRenderer)
class uAnalyserChannel : public QWidget
{
    Q_OBJECT

public:
    uAnalyserChannel(uAnalyserControls *parent_cont, uAnalyserWidget *main_widget, uAnalyserRenderer *rend, QVector<double> *time_sweep);
    ~uAnalyserChannel();

    void setTimeBase(qreal t_base); //set time base - does NOT update screen
    void recalculateTrace() {  trace->calculatePainterPath();  } //update state of corresponding uAnalyserChannelTrace
    QGroupBox *channelGroupBox; //box(frame) for this channel's all controls

private:
    uAnalyserChannelTrace *trace; //responsible for drawing input signal on screen
    uAnalyserWidget *mainWidget;
    uAnalyserRenderer *renderer; //area for displaing all channels

    QComboBox *colorCombo;
    QComboBox *yGainCombo;
    QPushButton *removeButton;

    QLabel *colorLabel;
    QLabel *gainLabel;
    QLabel *vpdLabel;

public:
    QVector<double> *timeSweep; //owned by uAnalyserWidget
    QVector<double> voltage; //samples of the input signal

private slots:
    void setColor(int i );
    void setYGain(int i );
    void remove() {  emit removeChannelPressed();  }

signals:
     void removeChannelPressed();
};

#endif // UANALYSERCHANNEL_H
